package com.Controller;

import com.Service.SubejctService;
import com.entity.PageInfo;
import com.entity.Subject;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    private SubejctService subejctService;

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Object insert(@RequestBody Subject subject){
        System.out.println(subject);
        return subejctService.insert(subject);
        /*if(subject.getTeaWorkPer()+subject.getStuWorkPer() ==1.00){
            subejctService.insert(subject);
            return "success";
        }
        else {
            return "failed(输入老师与学生工作量之和不为1)";
        }*/
    }

    @RequestMapping(value ="/delete/{id}",method = RequestMethod.DELETE)
    public Object delete(@PathVariable int id){
        int  record = subejctService.delete(id);
        if(record==0){
            return "failed";
        }
        return "success";
    }

    @RequestMapping(value = "/update/{id}",method = RequestMethod.PUT)
    public Object update(@RequestBody Subject subject,@PathVariable int id){
        //通过setId把id值拿到，这就不许在前端返回id值。
        subject.setId(id);
        int  record = subejctService.update(subject);
        if(record==0){
            return "failed";
        }
        return "success";
    }

    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public Object query(Subject subject){

       /* PageHelper.startPage(subject.getPage(),subject.getRows());
        List<Subject> schPlanList = subejctService.query(subject);
        PageInfo<Subject> subjectPageInfo = new PageInfo<>(schPlanList);
        return subjectPageInfo;*/
        List<Subject> schPlanList = subejctService.query(subject);
        return schPlanList;
    }
}
